package com.apimarket.controller;

import com.apimarket.entity.Result;
import com.apimarket.entity.kong.Plugin;
import com.apimarket.service.PluginService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;

/**
 * 处理plugin操作相关请求可控制层
 *
 * @author yyc
 */
@RestController
@RequestMapping("/plugin")
public class PluginController {

    @Autowired
    private PluginService pluginService;

    private static final String IO_ERROR = "网络异常";

    /**
     * 查找属于某个Route的所有plugin
     * @param id
     * @return
     */
    @RequestMapping("findRoutePlugins")
    public Result<List<Plugin>> findRoutePlugins(String id) throws IOException, URISyntaxException {
        try{
            List<Plugin> list=pluginService.findRoutePlugins(id);
            return new Result<>(list,200,"success");
        }catch(IOException e){
            return new Result<>(null,400,IO_ERROR);
        }

    }

    /**
     * 查找属于某个Service的所有plugin
     * @param id service的id
     * @return
     */
    @RequestMapping("findServicePlugins")
    public Result<List<Plugin>> findServicePlugins(String id) throws IOException, URISyntaxException {
        try{
            List<Plugin> list=pluginService.findServicePlugins(id);
            return new Result<>(list,200,"success");
        }catch(IOException e){
            return new Result<>(null,400,IO_ERROR);
        }

    }

    @RequestMapping("/findPageList")
    public Result<PageInfo<Map<String, Object>>> findPageList(Integer pageSize,Integer page,String name){
        PageInfo<Map<String, Object>> result=pluginService.findPageList(pageSize,page,name);
        return new Result<>(result,200,"success");
    }

    //类型转化
    public void judgeConfig(Map<String,Object> config){
        if(config.containsKey("allow")&&(config.get("allow") instanceof String)){
            //如果配置config中有allow，且传的allow是一个字符串类型
            //就把他转为数组，约定allow允许的用户组以逗号分隔
            String allowGroups = (String)config.get("allow");
            String[] groups = allowGroups.split(",");
            config.put("allow",groups);
        }if(config.containsKey("year")&&(config.get("year") instanceof String)){
            Integer year=Integer.parseInt((String)config.get("year"));
            config.put("year",year);
        }if(config.containsKey("month")&&(config.get("month") instanceof String)){
            Integer month=Integer.parseInt((String)config.get("month"));
            config.put("month",month);
        }if(config.containsKey("day")&&(config.get("day") instanceof String)){
            Integer day=Integer.parseInt((String)config.get("day"));
            config.put("day",day);
        }if(config.containsKey("hour")&&(config.get("hour") instanceof String)){
            Integer hour=Integer.parseInt((String)config.get("hour"));
            config.put("hour",hour);
        }if(config.containsKey("minute")&&(config.get("minute") instanceof String)){
            Integer minute=Integer.parseInt((String)config.get("minute"));
            config.put("minute",minute);
        }if(config.containsKey("second")&&(config.get("second") instanceof String)){
            Integer second=Integer.parseInt((String)config.get("second"));
            config.put("second",second);
        }
    }

    @RequestMapping(value = "/add", method = {RequestMethod.POST})
    public Result<String> addPlugin(@RequestBody @Validated Plugin plugin) throws URISyntaxException {
        try {
            Map<String, Object> config = plugin.getConfig();
            //key-auth插件传的config为null
            if(config!=null)
                judgeConfig(config);
            Plugin addResult=pluginService.addPlugin(plugin);
            return new Result<>("success", 200, "success");
        } catch (IOException e) {
            return new Result<>(IO_ERROR, 400, "fail");
        }
    }


    @RequestMapping(value = "/update", method = {RequestMethod.POST})
    public Result<String> updatePlugin(@RequestBody Plugin plugin) throws URISyntaxException {
        try {
            pluginService.updatePlugin(plugin);
            return new Result<>("success", 200, "success");
        } catch (IOException e) {
            return new Result<>(IO_ERROR, 400, "fail");
        }
    }

    @RequestMapping(value = "/delete", method = {RequestMethod.DELETE,RequestMethod.POST})
    public Result<String> deletePlugin(@RequestParam String id) throws URISyntaxException {
        try {
            pluginService.deletePlugin(id);
            return new Result<>("success", 200, "success");
        } catch (IOException e) {
            return new Result<>(IO_ERROR, 400, "fail");
        }
    }
    @RequestMapping("findServicePlugin")
    public Result<List<Plugin>> findServicePlugin(String id){
        List<Plugin> list=pluginService.findServicePlugin(id);
        return new Result<>(list,200,"success");
    }
}
